package com.frame.work.frame.deal.device0faa;

import com.alibaba.fastjson.JSONObject;
import com.frame.work.common.util.JsonToMqtt;
import com.frame.work.frame.deal.base.BaseFrame;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.Map;

@Data
public class ZhuoYueMianBan_0FAA8A0A extends BaseFrame {
    private String switchState;
    private String spreadingType;

    public ZhuoYueMianBan_0FAA8A0A(String gatewayMac, String data) {
        super(gatewayMac, data);

        this.setSwitchState(StringUtils.substring(data, data.length() - 2, data.length()));
        this.setSpreadingType(StringUtils.substring(data, 38, 40));

        String switchState_1 = StringUtils.substring(data, data.length() - 20, data.length() - 18);
        String switchState_2 = StringUtils.substring(data, data.length() - 18, data.length() - 16);
        String switchState_3 = StringUtils.substring(data, data.length() - 16, data.length() - 14);
        String switchState_4 = StringUtils.substring(data, data.length() - 14, data.length() - 12);

        String[] arr = {switchState_1, switchState_2, switchState_3, switchState_4};

        String switchCount = StringUtils.substring(data, 46, 48);
        String sceneCount = StringUtils.substring(data, 48, 50);
        Map<String, Object> actions = new HashMap<>(2);
        actions.put("controlType", "DIRECT_CONTROL");
        actions.put("switchCount", switchCount);
        actions.put("sceneCount", sceneCount);
        actions.put("multiSwitchStates", JSONObject.toJSONString(arr));
        this.setActions(actions);
        if (!StringUtils.equals("00", sceneCount)) {
            String jsonString = JSONObject.toJSONString(this);
            JsonToMqtt.sendMQTT(gatewayMac, jsonString);
        } else {
            String jsonString = JSONObject.toJSONString(this);
            JsonToMqtt.sendBaseMQTT(gatewayMac, jsonString);
        }


        String productType = this.getProductType();
        for (int i = 0; i < Integer.parseInt(switchCount); i++) {
            this.setProductType(productType + "_" + (i + 1));
            this.setSwitchState(arr[i]);
            Map<String, Object> actions1 = new HashMap();
            actions1.put("controlType", "DIRECT_CONTROL");
            actions1.put("switchState", arr[i]);
            this.setActions(actions1);
            //处理好的json对象
            String json1 = JSONObject.toJSONString(this);
            //将数据发送到mqtt队列中
            JsonToMqtt.mqttPushLianKongNew(gatewayMac, json1);
        }
    }
}
